package java6681.lesson04;

import java.util.*;

/**
 * @author Junichiro
 * @description
 * @date 2021-03-29 0:42
 **/

public class Foreach6681 {
    public static void main(String[] args) {
        /*创建若干个Student对象*/
        Student6681 stu1 = new Student6681("小张", 22);
        Student6681 stu2 = new Student6681("小王", 18);
        Student6681 stu3 = new Student6681("张张", 38);
        Student6681 stu4 = new Student6681("王五", 66);

        ArrayList<Student6681> stu = new ArrayList<Student6681>();


        /*添加到集合中*/
        stu.add(stu1);
        stu.add(stu2);
        stu.add(stu3);
        stu.add(stu4);

        useIterator6681(stu);
        useForeach6681(stu);
        useList6681(stu);
        useArray6681(stu);

    }
    /*用集合的迭代器遍历输出*/
    public static void useIterator6681(Collection<Student6681> stu) {
        Iterator<Student6681> it = stu.iterator();

        while (it.hasNext()) {
            Student6681 next = it.next();
            System.out.println(next);
        }

    }
    /*用增强for遍历输出*/
    public static void useForeach6681(Collection<Student6681> stu) {
        for (Student6681 student6681 : stu) {
            System.out.println(student6681);
        }

    }
    /*按序号遍历输出*/
    public static void useList6681(List<Student6681> stu) {
        for (int i = 0; i < stu.size(); i++) {
            Student6681 student6681 = stu.get(i);
            System.out.println(student6681);
        }

    }
    /*通过数组遍历输出*/
    public static void useArray6681(Collection<Student6681> stu) {
        Object[] objects = stu.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }
    }

}

class Student6681 {
    private String name;
    private int age;

    public Student6681() {
    }

    public Student6681(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name + "," + age;
    }
}
